﻿using System;
using Common.ParameterLocator;
using SpecFlowTfsConnector;
using SpecFlowTfsLinker.Entities;
using SpecFlowTfsLinker.VisualStudioHelper;
using SpecFlowTfsLinkerAddIn;

namespace Microsoft.SpecFlowTfsLinker_Extension
{
    public partial class SpecFlowTfsLinkerExtensionPackage
    {
        private UserStory _userStory;

        private void ExecuteExtension(object sender, EventArgs e)
        {
            try
            {
                InitializeParameter();
                LaunchSpecFlowSelectorUi();
                if (_userStory == null) return;
                var vsSpecFlowExtension = new VisualStudioSpecFlowExtensionHelper(_userStory, VisualStudioService);
                vsSpecFlowExtension.GenerateSpecFlow();
            }
            catch (Exception ex)
            {
                var box = new UserStorySelectorException(ex);
                box.ShowDialog();
            }
        }

        private void InitializeParameter()
        {
            ServiceParameter.Instance.Parameter = GetVisualStudioParameters();
        }

        private IParameter GetVisualStudioParameters()
        {
            var parameter = ((VisualStudioParameter)GetDialogPage(typeof(VisualStudioParameter)));
            return new VsParameter
                       {
                           TfsServerUrl = GetValue(parameter.TfsServerUrl, DefaultParameter.TfsServerUrl),
                           TfsUserNameConnexion = GetValue(parameter.TfsUsernameServer, DefaultParameter.TfsUserNameConnexion),
                           TfsPasswordConnexion = GetValue(parameter.TfsPasswordServer, DefaultParameter.TfsPasswordConnexion),
                           FolderName = GetValue(parameter.FolderName, DefaultParameter.FolderName),
                           FormatDisplayParameter =
                               GetValue(parameter.FormatDisplayParameter, DefaultParameter.FormatDisplayParameter),
                           FormatParameter = GetValue(parameter.FormatParameter, DefaultParameter.FormatParameter),
                           RegexFindParameter =
                               GetValue(parameter.RegexFindParameter, DefaultParameter.RegexFindParameter),
                           RegexCleanStatement =
                               GetValue(parameter.RegexCleanStatement, DefaultParameter.RegexCleanStatement),
                           RegexFindStatement =
                               GetValue(parameter.RegexFindStatement, DefaultParameter.RegexFindStatement),
                           TfsQueryUserStory = GetValue(parameter.TfsQueryUserStory, DefaultParameter.TfsQueryUserStory),
                           TfsWorkitemType = GetValue(parameter.TfsWorkitemType, DefaultParameter.TfsWorkitemType),
                           InconclusiveMessage =
                               GetValue(parameter.InconclusiveMessage, DefaultParameter.InconclusiveMessage),
                       };
        }

        private static string GetValue(string value, string defaultValue)
        {
            return string.IsNullOrEmpty(value) ? defaultValue : value;
        }

        private void LaunchSpecFlowSelectorUi()
        {
            try
            {
                var specFlowSelectorUi = new UserStorySelector(new TfsSpecFlowConnector());
                specFlowSelectorUi.ShowDialog();

                if (UiIsClosedWithoutSelectedItem(specFlowSelectorUi))
                    return;

                _userStory = specFlowSelectorUi.Model.SelectedUserStory;
            }
            catch (Exception ex)
            {
                var box = new UserStorySelectorException(ex);
                box.ShowDialog();
            }
        }

        private static bool UiIsClosedWithoutSelectedItem(UserStorySelector ui)
        {
            return !ui.DialogResult.HasValue || (!ui.DialogResult.Value && ui.Model.SelectedUserStory == null);
        }
    }
}